<template>
  <div class="login-page">
    <img src="../video/bg2.fc58c850.png" style="position:absolute;left:200px;top:220px;width:300px" alt="">
    <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <h1 style="text-align:center; color:#fff ;margin-bottom:30px">千峰管理系统</h1>
  <el-form-item label="用户名" prop="username" style="color:#fff">
    <el-input type="text" v-model="ruleForm.username" autocomplete="off"></el-input>
  </el-form-item>
  <el-form-item label="密码" prop="password">
    <el-input type="password" v-model="ruleForm.password" autocomplete="off"></el-input>
  </el-form-item>
 
  <el-form-item>
    <el-button type="primary" @click="submitForm('ruleForm')" @keydown.enter="submitForm('ruleForm')" style="width:88%;background: linear-gradient(90deg,#1596fb,#002dff)">提交</el-button>
    <!-- <el-button @click="resetForm('ruleForm')">重置</el-button> -->
  </el-form-item>
</el-form>
<video src="../video/bg_video.d2d602a9.mp4" muted="muted" autoplay loop preload="auto"></video>
  </div>
</template>

<script>
import {login} from "../../api/index"
import {mapMutations} from "vuex"
  export default {
    data() {
      // var checkAge = (rule, value, callback) => {
      //   if (!value) {
      //     return callback(new Error('年龄不能为空'));
      //   }
      //   setTimeout(() => {
      //     if (!Number.isInteger(value)) {
      //       callback(new Error('请输入数字值'));
      //     } 
      //     // else {
      //     //   if (value < 18) {
      //     //     callback(new Error('必须年满18岁'));
      //     //   } 
      //     //   else {
      //     //     callback();
      //     //   }
      //     // }
      //   }, 1000);
      // };
      var validateUsername = (rule, value, callback) => {
        //用户名正则，4到16位（字母，数字，下划线，减号）
        var uPattern = /^[a-zA-Z0-9_-]{3,16}$/;
        //输出 true
          //如果没通过正则，报错
        if (!uPattern.test(value)) {
          callback(new Error('请输入用户名'));
        } else {
          
          callback();
        }
        
      };
      var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        }  else {
          callback();
        }
      };
      return {
        ruleForm: {
          username: '',
          password: '',
          
        },
        rules: {
          username: [
            { validator: validateUsername, trigger: 'blur' }
          ],
          password: [
            { validator: validatePass, trigger: 'blur' }
          ]
        }
      };
    },
    methods: {
      ...mapMutations("user",['SETUSERINFO']),
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            //生效时，加载loading动画
            const loading = this.$loading({
          lock: true,
          text: 'Loading',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.7)'
        });
            
            let{username,password}= this.ruleForm
           
            login(username,password)
            .then(res=>{
               loading.close();
              console.log(res);
              if(res.data.state){
                this.$message({
          message: '恭喜你，成功登录',
          type: 'success'
        })
                localStorage.setItem("myToken",res.data.token)
                localStorage.setItem("userInfo",JSON.stringify(res.data.userInfo))
                this.SETUSERINFO(res.data.userInfo)
                this.$router.push("/home")
              }else{
                 this.$message.error("用户名或密码错误")
              }
              })
            .catch(err=>{
              console.log(err);
            })
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }
  }
</script>


